package com.huike.clues.service.Impl;

import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.dto.TbCluePageDto;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.TbClueService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TbClueServiceImpl implements TbClueService {
    @Resource
    private TbClueMapper tbClueMapper;
    @Resource
    private TbActivityMapper tbActivityMapper;
    @Resource
    private TbAssignRecordMapper tbAssignRecordMapper;

    /**
     * 查询线索管理列表接口
     *
     * @param tbCluePageDto
     */
    @Override
    public List<TbClue> selectTbClueList(TbCluePageDto tbCluePageDto) {

        return tbClueMapper.selectTbClueList(tbCluePageDto);

    }

    /**
     * 根据id获取线索基础信息
     *
     * @param id
     */
    @Override
    public TbClue selectCluesById(Long id) {

        TbClue tbClue = tbClueMapper.selectCluesById(id);
        //查询关联id后设置相关信息
        if (tbClue != null) {
            TbActivity activity = tbActivityMapper.selectById(tbClue.getActivityId());
            if (activity != null) {
                tbClue.setActivityInfo(activity.getInfo());
            }
        }
        TbAssignRecord tbAssignRecord = tbAssignRecordMapper.selectAssignRecordByAssignId(tbClue.getId());
        if (tbAssignRecord != null) {
            tbClue.setOwner(tbAssignRecord.getUserName());
            tbClue.setOwnerTime(tbAssignRecord.getCreateTime());
            return tbClue;
        }
        return tbClue;
    }

    /**
     * 查询线索池
     *
     * @param tbClue@return
     */
    @Override
    public List<TbClue> selectTbCluePool(TbClue tbClue) {

        List<TbClue> clueList = tbClueMapper.selectTbCluePool(tbClue);
        clueList.forEach(clue -> {
            if (clue.getActivityId() != null) {
                TbActivity activity = tbActivityMapper.selectById(clue.getActivityId());
                if (activity != null) {
                    clue.setActivityName(activity.getName());
                }
            }
        });
        return clueList;
    }

    /**
     * 批量捞取
     *
     * @param ids
     * @param userId
     */
    @Override
    public String gain(Long[] ids, Long userId) {
        return null;
    }

    /**
     * 新增线索管理
     *
     * @param tbClue
     */
    @Override
    public void addClues(TbClue tbClue) {
        Date date = DateUtils.getNowDate();
        tbClue.setCreateTime(date);
        //添加创建人
        String username = SecurityUtils.getUsername();
        tbClue.setCreateBy(username);
        tbClueMapper.insetTbClue(tbClue);
        //添加线索分配记录
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setAssignId(tbClue.getId());
        tbAssignRecord.setUserId(SecurityUtils.getUserId());
        tbAssignRecord.setUserName(SecurityUtils.getUsername());
        tbAssignRecord.setDeptId(SecurityUtils.getDeptId());
        tbAssignRecord.setCreateBy(SecurityUtils.getUsername());
        tbAssignRecord.setCreateTime(date);
        tbAssignRecordMapper.add(tbAssignRecord);

    }

    /**
     * 查询手机号
     *
     * @param phone
     * @return
     */
    @Override
    public boolean getPhone(String phone) {
        TbClue dbcule = tbClueMapper.getPhone(phone);
        if (dbcule == null) {
            return true;
        } else {
            return false;
        }
    }
}
